//
//  StoresDetailViewController.m
//  BMProject
//
//  Created by chris on 16/1/1.
//  Copyright © 2016年 BMProject. All rights reserved.
//

#import "StoresDetailViewController.h"
#import "DetailTopCell.h"
#import "DetailBottomCell.h"
#import "ReasonViewController.h"
#import "DataModel.h"
#import "InformationChannelCell.h"
#import "InformationBranchCell.h"
#import "InformationIdCell.h"
#import "InformationHandheldCell.h"
#import "UIViewController+common.h"

@interface StoresDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSDictionary *_dictionary1;
    NSDictionary *websiteinfo;
    NSString *_imgStr;
    NSString *_imgStrTwo;
    NSString *_imgStrThree;
    UITableView *_tableView;
    NSString *orderID;//工单id
}
@end

@implementation StoresDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"门店信息审核";
    orderID = _model.id;
    [self requestDetail];
}

//返回true代表这是第二次审核
- (BOOL)isCheckTwice{
    if(IsStringEmpty(self.model.auditor)){
        return NO;
    }else{
        return YES;
    }
}

#pragma mark-UITableViewDateSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if([@"1" isEqualToString:websiteinfo[@"mainType"]]) {//代表个人 个人多1个图片
        if([self isCheckTwice]){
            return 6;
        }else{
            return 5;
        }
    }else{
        if([self isCheckTwice]){
            return 5;
        }else{
            return 4;
        }
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if([@"1" isEqualToString:websiteinfo[@"mainType"]]) {//代表个人
        if (indexPath.section == 0) {
            static NSString *cellId = @"DetailTopCell";
            DetailTopCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setInfo:_dictionary1];
            cell.storesListModel = _model;
            return cell;
        }
        else if (indexPath.section == 1) {
            static NSString *cellId = @"InformationIdCell";
            InformationIdCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setInfo:_imgStr];
            return cell;
        }
        else if(indexPath.section == 2)
        {
            static NSString * cellId = @"InformationHandheldCell";
            InformationHandheldCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setInfo:_imgStrTwo];
            return cell;
        }
        else if (indexPath.section == 3) {
            static NSString *cellId = @"InformationHandheldCell";
            InformationHandheldCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_imgStrThree AndType:@"门店照片"];
            return cell;
        }else if(indexPath.section == 4){
            if([self isCheckTwice]){//第一次审核cell
                static NSString *cellId = @"InformationChannelCell";
                InformationChannelCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
                if (!cell) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                }
                [cell setCellInfo:_model];
                return cell;
            }else{//btn cell
                static NSString *cellId = @"DetailBottomCell";
                DetailBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
                if (!cell) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    [cell.passBtn addTarget:self action:@selector(passClick) forControlEvents:UIControlEventTouchUpInside];
                    [cell.rejectBtn addTarget:self action:@selector(rejectClick) forControlEvents:UIControlEventTouchUpInside];
                }
                return cell;
            }
        }else{//btn cell
            static NSString *cellId = @"DetailBottomCell";
            DetailBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                [cell.passBtn addTarget:self action:@selector(passClick) forControlEvents:UIControlEventTouchUpInside];
                [cell.rejectBtn addTarget:self action:@selector(rejectClick) forControlEvents:UIControlEventTouchUpInside];
            }
            return cell;
        }
    }else{//代表企业
        if (indexPath.section == 0) {
            static NSString *cellId = @"DetailTopCell";
            DetailTopCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setInfo:_dictionary1];
            cell.storesListModel = _model;
            return cell;
        }
        else if (indexPath.section == 1) {
            static NSString * cellId = @"InformationHandheldCell";
            InformationHandheldCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_imgStr AndType:@"营业执照扫描件"];
            return cell;
        }
        else if(indexPath.section == 2)
        {
            static NSString * cellId = @"InformationHandheldCell";
            InformationHandheldCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell)
            {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_imgStrTwo AndType:@"门店照片"];
            return cell;
        }
        else if (indexPath.section == 3) {
            if([self isCheckTwice]){//第一次审核cell
                static NSString *cellId = @"InformationChannelCell";
                InformationChannelCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
                if (!cell) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                }
                [cell setCellInfo:_model];
                return cell;
            }else{//btn cell
                static NSString *cellId = @"DetailBottomCell";
                DetailBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
                if (!cell) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    [cell.passBtn addTarget:self action:@selector(passClick) forControlEvents:UIControlEventTouchUpInside];
                    [cell.rejectBtn addTarget:self action:@selector(rejectClick) forControlEvents:UIControlEventTouchUpInside];
                }
                return cell;
            }
        }else{//btn cell
            static NSString *cellId = @"DetailBottomCell";
            DetailBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                [cell.passBtn addTarget:self action:@selector(passClick) forControlEvents:UIControlEventTouchUpInside];
                [cell.rejectBtn addTarget:self action:@selector(rejectClick) forControlEvents:UIControlEventTouchUpInside];
            }
            return cell;
        }
    }

}

#pragma mark- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([@"1" isEqualToString:websiteinfo[@"mainType"]]) {//代表个人 个人多1个图片
        if (indexPath.section == 0)
        {
            return 400;
        }else if(indexPath.section == 1 || indexPath.section == 2 || indexPath.section == 3){
            return 250;
        }else if(indexPath.section == 4){
            if([self isCheckTwice]){
                return 160;
            }else{
                return 60;
            }
        }else{
            return 60;
        }
    }else{
        if (indexPath.section == 0)
        {
            return 400;
        }else if(indexPath.section == 1 || indexPath.section == 2){
            return 250;
        }else if(indexPath.section == 3){
            if([self isCheckTwice]){
                return 160;
            }else{
                return 60;
            }
        }else{
            return 60;
        }
    }
}

#pragma mark-event pass
- (void)passClick
{
    DLog(@"审核通过..");
    [UserServices auditOptBytoken:[[AppDelegate appDelegate] getUserInfo].token
                               id:kIntToString(orderID.intValue)
                           status:@"pass"
                  completionBlock:^(int result, id responseObject) {
                      if (result == API_Request_Success) {
                          if ([responseObject isKindOfClass:[NSArray class]]) {
                                NSDictionary *dict = responseObject[0];
                                NSString *flag = dict[@"flag"];
                                NSString *msg  = dict[@"msg"];
                                if (flag.intValue == 0) {
                                    DLog(@"审核通过");
                                    [SVProgressHUD showSuccessWithStatus:msg];
                                    [self backButtonClick:nil];
                                } else {
                                    [SVProgressHUD showErrorWithStatus:[NSString ignoreEmptyStr:msg replaceStr:@"提交失败!!"]];
                                }
                          }
                      }
                  }];

}

#pragma mark-event rejet
- (void)rejectClick
{
    if (orderID == nil || orderID.intValue == 0) {
        return;
    }
    ReasonViewController * reason = [[ReasonViewController alloc]initWithNibName:@"ReasonViewController" bundle:nil];
    reason.orderID = orderID;
    reason.isFromeOrder = NO;
    [self.navigationController pushViewController:reason animated:YES];
}

- (void) requestDetail {
    [UserServices auditBytoken:[[AppDelegate appDelegate] getUserInfo].token
                            id:_model.id
               completionBlock:^(int result, id responseObject) {
                    if (result == API_Request_Success) {
                        _dictionary1 = responseObject[0];

                        NSDictionary *imgMap = _dictionary1[@"imgMap"];
                        websiteinfo = _dictionary1[@"websiteInfo"];
                        NSDictionary *sysUnit = _dictionary1[@"sysUnit"];

                        if([@"1" isEqualToString:websiteinfo[@"mainType"]]){//代表个人
                            NSDictionary *keyOne = imgMap[@"PERSONAGE_KEY_ONE"];
                            NSDictionary *keyTwo = imgMap[@"PERSONAGE_KEY_TWO"];
                            NSDictionary *keyThree = imgMap[@"LEASE_KEY"];
                            NSString *threePath = keyThree[@"fileWebPath"];
                            _imgStrThree = [BASE_IMG_URL stringByAppendingString: [NSString ignoreEmptyStr:threePath]];
                            NSString *fileWebPath = keyOne[@"fileWebPath"];
                            NSString *filePath = keyTwo[@"fileWebPath"];
                            if(!IsStringEmpty(_dictionary1[@"auditor"])){
                                _model.auditor = _dictionary1[@"auditor"];
                            }
                            if(!IsStringEmpty(_dictionary1[@"auditorSecond"])){
                                _model.auditorSecond = _dictionary1[@"auditorSecond"];
                            }
                            _imgStr = [BASE_IMG_URL stringByAppendingString: fileWebPath];
                            _imgStrTwo = [BASE_IMG_URL stringByAppendingString: [NSString ignoreEmptyStr:filePath]];
                        }else{//代表企业
                            NSDictionary *keyOne = imgMap[@"COMPANY_KEY"];
                            NSDictionary *keyTwo = imgMap[@"LEASE_KEY"];
                            NSString *fileWebPath = keyOne[@"fileWebPath"];
                            NSString *filePath = keyTwo[@"fileWebPath"];
                            if(!IsStringEmpty(_dictionary1[@"auditor"])){
                                _model.auditor = _dictionary1[@"auditor"];
                            }
                            if(!IsStringEmpty(_dictionary1[@"auditorSecond"])){
                                _model.auditorSecond = _dictionary1[@"auditorSecond"];
                            }
                            _imgStr = [BASE_IMG_URL stringByAppendingString: fileWebPath];
                            _imgStrTwo = [BASE_IMG_URL stringByAppendingString: [NSString ignoreEmptyStr:filePath]];
                        }
                        UITableView * tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
                        tableview.delegate = self;
                        tableview.dataSource = self;
                        tableview.sectionHeaderHeight = 0;
                        tableview.sectionFooterHeight = 0;
                        tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
                        tableview.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 10)];
                        _tableView = tableview;
                        [self addSubview:tableview];
                        [_tableView reloadData];
                    }
               }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
